//
//  JZHFrenListTableViewCell.m
//  极致荟
//
//  Created by hetao on 14-5-10.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHFrenListTableViewCell.h"
#import "MMTextAttachment.h"

@implementation JZHFrenListTableViewCell
NSMutableAttributedString *string;
- (void)awakeFromNib
{
    [self.headImage.layer setCornerRadius:6];
    [self.headImage.layer setMasksToBounds:YES];
    [self.nuber.layer setCornerRadius:7.5];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)addCellData:(OtherUsers *)ouser
{
    if(ouser.nubmer==0)
    {
        self.nuber.hidden=YES;
    }else
    {
        self.nuber.hidden=NO;
        self.nuber.text=[NSString stringWithFormat:@"%d", ouser.nubmer ];
    }
    [ImageCache imageUrl:ouser.userHeadImage UiImageview:self.headImage];
    if(ouser.bname && ouser.bname.length!=0)
    {
        self.ousername.text=ouser.bname;
    }else
    {
        self.ousername.text=ouser.name;
    }
    [self.ousername sizeToFit];
    [self.vipicon setFrame:CGRectMake(self.ousername.frame.origin.x+self.ousername.frame.size.width+5, self.vipicon.frame.origin.y, 18, 18)];
    [self vipiconimage:ouser];
    if(ouser.note.noteType==IMAGE)
    {
        self.content.text=@"[图片]";
        return;
    }
    
    if(ouser.note.noteType==VOICE)
    {
        self.content.text=@"[语音]";
        return;
    }
    if(ouser.note.noteType==BC)
    {
        self.content.text=@"[名片]";
        return;
    }
    [self faceDolo:ouser.note.content];
}
//会员图标显示设置
- (void)vipiconimage:(OtherUsers *) ouser
{
    int vip=ouser.vip;
    if(vip==0)
    {
        if(ouser.sex==1)
        {
            self.vipicon.image=[UIImage imageNamed:@"no1-1"];
        }else
        {
            self.vipicon.image=[UIImage imageNamed:@"no0-1"];
        }
        
        return;
    }
    
    if(vip==1)
    {
        self.vipicon.image=[UIImage imageNamed:@"vip1-1"];
    }else
    {
        self.vipicon.image=[UIImage imageNamed:@"vip2-1"];
    }
}

///表情替换  实现的基本逻辑是，先根据字符表情串，找出它的位置，长度为1
- (void)faceDolo:(NSString *)content
{
    NSArray *farr=[content componentsSeparatedByString:@";"];//根据‘;’截取出对应的数组
    
    NSMutableArray *fmuarr=[[NSMutableArray alloc]init];//用于保存最终截取到的表情字符
    for(NSString *str in farr)
    {
        NSArray *arr=[str componentsSeparatedByString:@"/"];//从‘;’中得到的数组在根据‘/’截取
        // NSLog(@"%@",arr);
        for(NSString *str2 in arr)
        {
            if(str2.length==2 || str2.length==3)//从‘/’中的截取结果来判断是否是表情字符
            {
                NSString *esc=[str2 substringToIndex:1];
                if([esc isEqualToString:@"f"])
                {
                    NSString *zostr=[str2 substringFromIndex:1];
                    [fmuarr addObject:zostr];
                }
            }
        }
        
    }
    
    
    NSString *zzstr=content;//存不含字符表情的纯文本
    for(NSString *ths in fmuarr)
    {
        zzstr=[zzstr stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"/f%@;",ths] withString:@""];
    }
    
    string=nil;
    string=[[NSMutableAttributedString alloc]initWithString:zzstr attributes:nil];
    
    NSString *maxcon=content;//用于装载替换过后的文本
    
    for(int i=0;i<fmuarr.count;i++)//查找表情字符的位置，
    {
        NSString *indexstr=fmuarr[i];
        maxcon=[self stringIndex:maxcon tostring:indexstr];
    }
    
    self.content.attributedText = string;
    
}


//根据给的文本内容查找出某个字符表情的位置
- (NSString *)stringIndex:(NSString *)str tostring:(NSString *)tostr
{
    NSString*tstr;//用于存放替换过的文本
    
    NSRange range;
    range = [str rangeOfString:[NSString stringWithFormat:@"/f%@;",tostr]];
    
    if (range.location != NSNotFound)
    {
        //如果在文本中找到该表情字符,则用一个字符替代，这样做的目的只是为后面的表情字符找到准确的位置，本身不用来显示成最终文本
        tstr = [str stringByReplacingCharactersInRange:range withString:@"*"];
        
        //开始生成表情图片并添加到文本
        MMTextAttachment *atac=[[MMTextAttachment alloc]initWithData:nil ofType:nil];
        UIImage * smileImage = [ UIImage imageNamed:[NSString stringWithFormat:@"%@f.gif",tostr]];
        atac.image=smileImage;
        NSAttributedString * textAttachmentString = [ NSAttributedString attributedStringWithAttachment:atac];
        
        [string insertAttributedString:textAttachmentString atIndex:range.location ] ;
    }
    return tstr;
}

@end
